<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮件主题" prop="emailSubject">
        <el-input
          v-model="queryParams.emailSubject"
          placeholder="请输入邮件主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发件邮箱" prop="emailSenderName">
        <el-input
          v-model="queryParams.emailSenderName"
          placeholder="请输入发件邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收件邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入收件邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择发送状态" clearable>
          <el-option label="未发送" value="0" />
          <el-option label="发送中" value="1" />
          <el-option label="发送成功" value="2" />
          <el-option label="发送失败" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间" prop="recipientSendTime">
        <el-date-picker clearable
                        v-model="queryParams.recipientSendTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择发送开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="sendEmail"
          :disabled="multiple"
        >重新推送</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emailrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务编码" align="center" prop="taskId" />
      <el-table-column label="任务名称" align="center" prop="taskName" show-overflow-tooltip/>
      <el-table-column label="邮件主题" align="center" prop="emailSubject" show-overflow-tooltip/>
      <el-table-column label="发件人" align="center" prop="emailSenderName" width="160"/>
      <el-table-column label="收件人" align="center" prop="customerName" show-overflow-tooltip/>
      <el-table-column label="收件邮箱" align="center" prop="email" width="180"/>
      <el-table-column label="发送状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)" size="mini">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column >
      <el-table-column label="发送时间" align="center" prop="recipientSendTime" width="180">
        <template slot-scope="scope">
          {{ parseTime(scope.row.recipientSendTime, '{y}-{m}-{d} {h}:{i}') || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="错误信息" align="center" prop="errorMsg" show-overflow-tooltip/>
      <el-table-column v-if="false" label="模板ID" align="center" prop="templateId" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>

import {listTaskRecipient} from "@/api/email/emailTask";
import { parseTime } from "@/utils/ruoyi";

export default {
  name: "EmailRecords",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 邮件发送记录表格数据
      emailrecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        taskName: null,
        emailContent: null,
        emailSubject: null,
        emailSenderName: null,
        email: null,
        status: null,
        recipientSendTime: null,
        customerName: null,
        errorMsg: null,
        templateId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskId: [
          { required: true, message: "任务ID不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "发送状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询邮件发送记录列表 */
    getList() {
      this.loading = true;
      listTaskRecipient(this.queryParams).then(response => {
        this.emailrecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error('获取邮件记录失败:', error);
        this.loading = false;
        this.$modal.msgError("获取邮件记录失败");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recipientId: null,
        taskId: null,
        taskName: null,
        emailContent: null,
        emailSubject: null,
        emailSenderName: null,
        email: null,
        status: null,
        recipientSendTime: null,
        customerName: null,
        errorMsg: null,
        templateId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recipientId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 重新推送邮件
    sendEmail() {
      if (this.ids.length === 0) {
        this.$modal.msgWarning("请选择要重新推送的邮件记录");
        return;
      }
      this.$modal.confirm('是否确认重新推送选中的邮件记录？').then(() => {
        // TODO: 实现重新推送逻辑
        this.$modal.msgSuccess("重新推送成功");
        this.getList();
      }).catch(() => {});
    },
    // 获取状态类型
    getStatusType(status) {
      switch (status) {
        case '0':
          return 'info';
        case '1':
          return 'warning';
        case '2':
          return 'success';
        case '3':
          return 'danger';
        default:
          return 'info';
      }
    },
    // 获取状态文本
    getStatusText(status) {
      switch (status) {
        case '0':
          return '未发送';
        case '1':
          return '发送中';
        case '2':
          return '发送成功';
        case '3':
          return '发送失败';
        default:
          return status || '未知';
      }
    },
    // 时间格式化
    parseTime
  }
};
</script>
